from sqlalchemy import Column, Integer, String, JSON, Text, DateTime, Boolean, Float, ForeignKey
from sqlalchemy.orm import relationship
from .database import DB1


class Users(DB1):
    """用户"""
    __tablename__ = "users"
    id = Column(Integer, primary_key=True, index=True, autoincrement=True)
    created_on = Column(DateTime)
    created_by = Column(Integer)
    updated_on = Column(DateTime)
    updated_by = Column(Integer)
    nickname = Column(String(100), nullable=False)
    username = Column(String(100), nullable=False)
    password = Column(String(100), nullable=False)
    avatar = Column(Text)
    last_login_time = Column(DateTime)
    permissions = Column(JSON, default="[]")
    roles = Column(JSON, default="[]")
    status = Column(Integer, default=1)  # 0:已删除，1:正常使用，2:锁定


class UserRole(DB1):
    """用户角色"""
    __tablename__ = "user_role"
    id = Column(Integer, primary_key=True, autoincrement=True)
    created_on = Column(DateTime)
    created_by = Column(Integer)
    updated_on = Column(DateTime)
    updated_by = Column(Integer)
    name = Column(String(100), nullable=False)
    permissions = Column(JSON, nullable=False, default="[]")
    status = Column(Integer, nullable=False, default=1)


class Flower(DB1):
    """鲜花"""
    __tablename__ = "flower"
    id = Column(Integer, primary_key=True, autoincrement=True)
    created_on = Column(DateTime)
    created_by = Column(Integer)
    updated_on = Column(DateTime)
    updated_by = Column(Integer)
    status = Column(Integer, nullable=False, default=1)  # 0-删除 1-正常 2-下架
    name = Column(String(100), nullable=False)
    description = Column(String(500))
    images = Column(JSON, default="[]")
    original_price = Column(Float, nullable=False)
    price = Column(Float, nullable=False)
    receiver = Column(Integer)
    material = Column(Integer)
    size = Column(Integer)
    type = Column(Integer, nullable=False)
    occasion = Column(Integer)
    tags = Column(JSON, default="[]")
    sales_count = Column(Integer, default=0)
    views_count = Column(Integer, default=1)
    presale = Column(Boolean, default=False)  # 预售
    presale_start_date = Column(DateTime)
    presale_end_date = Column(DateTime)
    presale_delivery_start_date = Column(DateTime)
    presale_delivery_end_date = Column(DateTime)


class FlowerType(DB1):
    """鲜花分类"""
    __tablename__ = "flower_type"
    id = Column(Integer, primary_key=True, index=True, autoincrement=True)
    created_on = Column(DateTime)
    created_by = Column(Integer)
    updated_on = Column(DateTime)
    updated_by = Column(Integer)
    status = Column(Integer, nullable=False, default=1)
    name = Column(String(100))  # 分类名称


class Customer(DB1):
    """客户"""
    __tablename__ = "customer"
    id = Column(Integer, primary_key=True, index=True, autoincrement=True)
    created_on = Column(DateTime)
    created_by = Column(Integer)
    updated_on = Column(DateTime)
    updated_by = Column(Integer)
    status = Column(Integer, nullable=False, default=1)
    openid = Column(String(255), nullable=False, unique=True)
    nickname = Column(String(255), nullable=False)
    level = Column(Integer, default=1)
    avatar = Column(String(255))
    phone = Column(String(20))
    gender = Column(Integer, default=-1)
    last_login_time = Column(DateTime)


class ShippingAddress(DB1):
    """收货地址"""
    __tablename__ = "shipping_address"
    id = Column(Integer, primary_key=True, index=True, autoincrement=True)
    created_on = Column(DateTime)
    created_by = Column(Integer)
    updated_on = Column(DateTime)
    updated_by = Column(Integer)
    status = Column(Integer, nullable=False, default=1)
    customer_id = Column(Integer, nullable=False)
    recipient = Column(String(255), nullable=False)
    phone = Column(String(20), nullable=False)
    province = Column(String(100), nullable=False)
    city = Column(String(100))
    district = Column(String(100))
    details_address = Column(String(255))
    is_default = Column(Boolean, default=False)


class ShoppingCart(DB1):
    """购物车"""
    __tablename__ = "shopping_cart"
    id = Column(Integer, primary_key=True, index=True, autoincrement=True)
    created_on = Column(DateTime)
    created_by = Column(Integer)
    updated_on = Column(DateTime)
    updated_by = Column(Integer)
    status = Column(Integer, nullable=False, default=1)
    customer_id = Column(Integer, nullable=False)
    flower_id = Column(Integer, ForeignKey("flower.id"), nullable=False)
    quantity = Column(Integer, nullable=False, default=1)
    flower = relationship("Flower", foreign_keys="ShoppingCart.flower_id")


class Order(DB1):
    """订单"""
    __tablename__ = "order"
    id = Column(Integer, primary_key=True, index=True, autoincrement=True)
    created_on = Column(DateTime)
    created_by = Column(Integer)
    updated_on = Column(DateTime)
    updated_by = Column(Integer)
    status = Column(Integer, nullable=False, default=1)  # 0-删除 1-正常 2-完成 3-关闭
    customer_id = Column(Integer, ForeignKey("customer.id"), nullable=False)
    customer_phone = Column(String(20))
    order_code = Column(String(255), nullable=False)
    items = Column(JSON)
    delivery_address = Column(JSON)
    delivery_method = Column(Integer, default=1)  # 1-配送, 2-自提
    delivery_person = Column(JSON)
    delivery_date = Column(DateTime)  # 发货日期
    presale = Column(Boolean)  # 预售订单
    presale_delivery_date = Column(DateTime)  # 预售交货日期
    greeting_card = Column(Text)
    notes = Column(Text)
    subtotal = Column(Float, nullable=False)
    total_price = Column(Float, nullable=False)
    payment_date = Column(DateTime)  # 支付时间
    payment_status = Column(Integer, default=1)  # 1-待支付 2-已支付 3-已退款
    shipping_status = Column(Integer, default=1)  # 1-待发货 2-已发货 3-已送达

    customer = relationship("Customer", foreign_keys="Order.customer_id")


class DeliveryPerson(DB1):
    """配送员"""
    __tablename__ = "delivery_person"
    id = Column(Integer, primary_key=True, index=True, autoincrement=True)
    created_on = Column(DateTime)
    created_by = Column(Integer)
    updated_on = Column(DateTime)
    updated_by = Column(Integer)
    status = Column(Integer, nullable=False, default=1)
    name = Column(String(255), nullable=False)
    phone = Column(String(20), nullable=False)
    delivering = Column(Integer, nullable=False, default=0)
    delivery_count = Column(Integer, nullable=False, default=0)


class Images(DB1):
    """图片"""
    __tablename__ = "images"
    id = Column(Integer, primary_key=True, index=True)  # 图片ID
    tags = Column(JSON, default=[])  # 图片标签
    folder = Column(Integer, nullable=False)  # 所属文件夹
    upload_time = Column(DateTime)  # 图片上传日期
    update_time = Column(DateTime, nullable=True)  # 更新日期
    width = Column(Integer)  # 图片宽度
    height = Column(Integer)  # 图片高度
    size = Column(Integer)  # 图片大小
    format = Column(String(10), nullable=False)  # 图片格式: webp,svg,gif 其它格式png,jpg都会转换成webp
    hash = Column(String(255), nullable=False)  # 哈希值


class ImagesFolder(DB1):
    """图片文件夹"""
    __tablename__ = "images_folder"
    id = Column(Integer, primary_key=True)  # ID
    name = Column(String(255), nullable=False)  # 名称
    created_time = Column(DateTime)  # 创建时间
    updated_time = Column(DateTime, nullable=True)  # 更新时间
    description = Column(Text)  # 描述
    count = Column(Integer, default=0)  # 图片数量
    size = Column(Integer, default=0)  # 存储大小
    operable = Column(Boolean, default=True)  # True:可操作的,False:禁止操作,一般由系统自动管理


class Article(DB1):
    """文章"""
    __tablename__ = "article"
    id = Column(Integer, primary_key=True, index=True, autoincrement=True)
    created_on = Column(DateTime)
    created_by = Column(Integer)
    updated_on = Column(DateTime)
    updated_by = Column(Integer)
    status = Column(Integer, nullable=False, default=1)
    title = Column(String(255))  # 标题
    abstract = Column(Text)  # 摘要
    content = Column(Text)  # 内容
    tags = Column(JSON, default="[]")  # 标签
    author = Column(String(255))  # 作者
    avatar = Column(String(255))
    cover = Column(String(255))  # 展示时使用的封面
    views_count = Column(Integer, default=0)  # 阅读量
    likes_count = Column(Integer, default=0)  # 点赞量


class ArticleLike(DB1):
    """文章点赞"""
    __tablename__ = "article_like"
    id = Column(Integer, primary_key=True, index=True, autoincrement=True)
    created_on = Column(DateTime)
    created_by = Column(Integer)
    updated_on = Column(DateTime)
    updated_by = Column(Integer)
    status = Column(Integer, nullable=False, default=1)
    customer_id = Column(Integer, nullable=False)
    article_id = Column(Integer, nullable=False)


class ArticleComment(DB1):
    """文章评论"""
    __tablename__ = "article_comment"
    id = Column(Integer, primary_key=True, index=True, autoincrement=True)
    created_on = Column(DateTime)
    created_by = Column(Integer)
    updated_on = Column(DateTime)
    updated_by = Column(Integer)
    status = Column(Integer, nullable=False, default=1)
    customer_id = Column(Integer, ForeignKey("customer.id"), nullable=False)
    article_id = Column(Integer, nullable=False)
    parent_id = Column(Integer)
    likes_count = Column(Integer, default=0)
    content = Column(Text)
    customer = relationship("Customer", foreign_keys="ArticleComment.customer_id")


class ArticleCommentLike(DB1):
    """文章评论点赞"""
    __tablename__ = "article_comment_like"
    id = Column(Integer, primary_key=True, index=True, autoincrement=True)
    created_on = Column(DateTime)
    created_by = Column(Integer)
    updated_on = Column(DateTime)
    updated_by = Column(Integer)
    status = Column(Integer, nullable=False, default=1)
    customer_id = Column(Integer, nullable=False)
    comment_id = Column(Integer, nullable=False)


class Config(DB1):
    """配置"""
    __tablename__ = "config"
    id = Column(Integer, primary_key=True, index=True, autoincrement=True)
    created_on = Column(DateTime)
    created_by = Column(Integer)
    updated_on = Column(DateTime)
    updated_by = Column(Integer)
    status = Column(Integer, nullable=False, default=1)
    name = Column(String(255), nullable=False, unique=True)
    data = Column(JSON, default="[]")
